package com.example.design.singletonDemo;

/**
 * 双重检锁
 */
public class Singleton4 {

    private static Singleton4 singleton4;

    private static Singleton4 getSingleton4() {
        if (singleton4 == null) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (Singleton4.class) {
                if (singleton4 == null) {
                    singleton4 = new Singleton4();
                }
            }
        }
        return singleton4;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Singleton4 singleton4 = Singleton4.getSingleton4();
                System.out.println(Thread.currentThread().getName() + "-------" + singleton4);
            }).start();
        }
    }
}
